package com.itheima.mapper;

import com.itheima.pojo.*;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select *from user where userName=#{name} and password=#{password}")
    public User Ulogin(User user);
    @Select("select * from user where userName=#{name}")
    public User findName(String name);
    @Insert("insert into user (password,userName) values (#{password},#{name})")
    public void registered(User user);
    @Select("select image from user where userName=#{userName}")
    public String getImage(String userName);
    @Select("select * from user where userName=#{userName}")
    public UserMessage getUserMessage(String userName);
    @Update("update user set userName=#{userName},password=#{password},address=#{address},image=#{image} where userId=#{userId}")
    public void changeUserMessage(UserMessage userMessage);
    @Select("select * from user where userName=#{name} and userId!=#{userId}")
    public User findName2(String name,int userId);
//    @Select("SELECT food.foodName,category,price,imgine,count FROM food LEFT JOIN (SELECT * FROM shoppinglist WHERE userName =#{userName} )AS filtered_list ON food.foodName = filtered_list.foodName where foodState='未下架';")
    public List<Shopping> uShowFood(String category,String foodName,String userName);
    @Update("update shoppinglist set count=count-1 where foodName=#{foodName} and userName=#{userName}")
    public void minusFood(String userName,String foodName);
    @Select("select * from shoppinglist where foodName=#{foodName} and userName=#{userName}")
    public ShoppingList findFood(String userName,String foodName);
    @Update("update shoppinglist set count=count+1 where foodName=#{foodName} and userName=#{userName}")
    public void addCount(String userName,String foodName);
    @Insert("insert into shoppinglist(userName,foodName,count) values (#{userName},#{foodName},1);")
    public void initCount(String userName,String foodName);
    @Select("select * from shoppinglist where userName=#{userName} and count!=0")
    public List<ShoppingList> getList(String userName);
    @Select("select address from user where userName=#{userName}")
    public String getAddress(String userName);
    @Delete("delete from shoppinglist where userName=#{userName}")
    public void deleteShoppinglist(String userName);
    @Insert("insert into orders (userName,foodOrder,address,remark,orderTime,orderState) values (#{userName},#{list},#{address},#{remark},#{localDateTime},'未接单')")
    public void submitOrder(String userName, String list, String address, String remark, LocalDateTime localDateTime);

    @Select("select count(*) from orders where userName=#{userName}")
    public int conCount(String userName);

    @Select("select orders.orderId,foodOrder,remark,orderTime,empName,orderState,rate from orders left join emporders on orders.orderId=emporders.orderId where orders.userName=#{userName} order by orders.orderId desc limit #{start},#{pageSize}")
    public List<UserOrders> conPage(String userName,int start,int pageSize);

    @Update("update emporders set rate=#{rate} where orderId=#{orderId}")
    public void giveRate(Long orderId,int rate);

    @Select("select orders.orderId  from orders left join emporders on orders.orderId=emporders.orderId where userName=#{userName} and orderState='取消订单' and (orders.knowcancel = 0 OR orders.knowcancel IS NULL)")
    public List<Long> ifCancel(String name);
    @Update("update orders set knowcancel=1 where orderId=#{orderId}")
    public void knowCancel(Long orderId);
}
